package com.goods.business.service.imp;

import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public Map productCategoryTreeNodeVOS(Integer pageNum,Integer pageSize) {
        List<ProductCategory> productCategories = productCategoryMapper.categoryTree();
        //List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        //转成vo
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.productCategoryTreeNodeVO(productCategories);
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);

        int total = productCategoryTreeNodeVOS.size();
        Map map = new HashMap();
        if (pageSize == null || pageNum == null){
            map.put("rows",build);
            map.put("total",total);
        }else {
            List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize, pageNum);

            map.put("rows", page);
            map.put("total", total);
        }
        return map;
    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        //转成vo
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.productCategoryTreeNodeVO(productCategories);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);

        return productCategoryTreeNodeVOList;
    }

    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        if (productCategoryVO != null){
            ProductCategory productCategory = new ProductCategory();
            BeanUtils.copyProperties(productCategoryVO,productCategory);
            productCategory.setCreateTime(new Date());
            productCategory.setModifiedTime(new Date());
            productCategoryMapper.insert(productCategory);
        }
    }

    @Override
    public ProductCategoryVO edit(Long id) {

        ProductCategory productCategory = productCategoryMapper.edit(id);
        //转成vo
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);

        return productCategoryVO;
    }

    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public void deleteById(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPid(id);

        if (productCategory == null) {
            productCategoryMapper.deleteByPrimaryKey(id);
        }
    }
}
